home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Demos / Codeworks 0.94b3 / Codeworks® WWW Demo Doc. / Scripting Manual.doc / Scripting Manual.doc.rsrc / TEXT_136.txt < prev    next >
Encoding:
Text File  |  1995-05-01  |  3.6 KB  |  61 lines

  1. Control Structures - For
  2.  
  3.     A for statement causes a block of expressions to be execute several times.  Each time through the block of expressions, a local variable is set to a different number.  For example:
  4.  
  5.         $ s.
  6.         s := 0.
  7.         for 1 to 10 do [ $ index i.
  8.             s := s + i.
  9.         ].
  10.         return s.                    Ô¨Å 55
  11.  
  12.     This sums the numbers from 1 to 10.  Each time through the loop (each iteration), the local variable i is set to a number from 1 to 10.  Then the expression adding it to s is executed.  The variable i is called the index variable. 
  13.  
  14. ¬ª    The declaration of the local variable i requires the bold word index.  Actually, i is an argument (a type of local variable) to the block.  Arguments are discussed later.  For now, just use this kind of declaration for the local variable in a for loop.
  15.  
  16.  
  17. For Parameters
  18.     The for statement can take an optional argument, by, that determines how much to bump the index variable each time.  Before executing the next example, open the transcript window.
  19.  
  20.     To open the transcript window:
  21. ‚Ä¢    Tap on Transcript‚Ķ in the System menu.  The transcript window opens.  You can move and resize this window as needed.
  22.  
  23.     Execute the following example:
  24.  
  25.         for 1900 to 2000 by 10 do [ $ index year.
  26.             $ is-mult-of-4, is-century.
  27.             is-mult-of-4 := year % 4 == 0.
  28.             is-century := year % 100 == 0.
  29.             if (is-mult-of-4 and (not is-century))
  30.                 then [ transcript put year.name && "is a leap-year" ]
  31.                 else [ transcript put year.name && "is not a leap-year" ].
  32.         ].
  33.     The block of expressions gets executed once for each decade from 1900 to 2000.  The transcript will show these years.  This example has several important things to notice:
  34.  
  35. ‚Ä¢    The index variable can be called anything you like.  In this case it was called year.
  36.  
  37. ‚Ä¢    Inside a block, there can be additional local variables.
  38.  
  39. ‚Ä¢    The % operator computes the remainder of the division between its two arguments.  12 % 5 is 2 because the remainder of 12 divided by 5 is 2.
  40.  
  41. ‚Ä¢    The put message, used with transcript as the receiver, appends a string to the transcript window.  This is useful for debugging, and seeing how your scripts are running.
  42.  
  43.  
  44. Local Variables in Blocks
  45.     In the example above, two local variables were declared inside the block of the for loop.  These variables are available only within the block.  They are created fresh each time the block is executed, each time through the loop.  After the block is done, the variables are no longer available.  This is true of the index variable as well.  For example:
  46.  
  47.         for 10 to 20 do [ $ index i. ]    an empty block
  48.         "the final value of i is" && i.name.
  49.  
  50.     This code won‚Äôt execute.  If you try to, the system will give you the error message ‚ÄúUnknown local or property‚Äù because the index variable i is not valid outside the block.
  51.  
  52.     What happens if the name of a local variable is the same as the name of a variable outside the block?  While this isn‚Äôt recommended, it works the following way:   Inside the block, the name refers to a new local variable;  outside the block it refers to the variable it would if the block wasn‚Äôt there.  For example:
  53.  
  54.     a := "Hello".    this sets the workspace variable
  55.  for 1 to 10 do [ $ index i.
  56.         $ a.    declares a local variable
  57.         a := i * i.    sets the local variable
  58.     ].
  59.     a := a && "kitty".    access and sets the workspace variable
  60.  
  61. ¬ª    A word about speed: Don‚Äôt worry about the expense in time of the system creating local variables each time through the loop.  Creating local variables is essentially free.  In fact variables outside the block are a little more expensive, as well as harder to read.  The moral: use local variables in blocks whenever possible.